package cn.edu.baiyunu.myapplication.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

// 亮色主题
private val LightColorScheme = lightColorScheme(
    primary = BudgetBlue,
    onPrimary = Color.White,
    primaryContainer = BudgetLightBlue,
    onPrimaryContainer = Color(0xFF001F3F),
    secondary = BudgetYellow,
    onSecondary = Color(0xFF332C00),
    secondaryContainer = BudgetYellow.copy(alpha = 0.2f),
    onSecondaryContainer = Color(0xFF332C00),
    tertiary = BudgetGreen,
    onTertiary = Color.White,
    background = Color.Transparent,
    onBackground = Color.White,
    surface = GlassSurface,
    onSurface = Color.White,
    error = BudgetRed,
    onError = Color.White
)

// 暗色主题
private val DarkColorScheme = darkColorScheme(
    primary = BudgetBlueDark,
    onPrimary = Color.White,
    primaryContainer = BudgetLightBlueDark,
    onPrimaryContainer = Color(0xFFD6E2FF),
    secondary = BudgetYellowDark,
    onSecondary = Color.Black,
    secondaryContainer = BudgetYellowDark.copy(alpha = 0.2f),
    onSecondaryContainer = Color(0xFFFFEDCA),
    tertiary = BudgetGreenDark,
    onTertiary = Color.White,
    background = Color.Transparent,
    onBackground = Color.White,
    surface = GlassSurfaceDark,
    onSurface = Color.White,
    error = BudgetRedDark,
    onError = Color.White
)

@Composable
fun BudgetAppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // 控制是否使用动态色彩（Android 12及以上支持）
    dynamicColor: Boolean = false, // 默认不使用动态色彩以保持统一外观
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    
    // 设置系统状态栏和导航栏为沉浸式
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            
            // 设置状态栏和导航栏透明
            window.statusBarColor = Color.Transparent.toArgb()
            window.navigationBarColor = Color.Transparent.toArgb()
            
            // 确保内容延伸到系统栏区域
            WindowCompat.setDecorFitsSystemWindows(window, false)
            
            // 设置状态栏图标和导航栏按钮为亮色
            WindowCompat.getInsetsController(window, view).apply {
                isAppearanceLightStatusBars = false
                isAppearanceLightNavigationBars = false
            }
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

// 为了向后兼容，保留旧的主题名称
@Composable
fun MyApplicationTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    dynamicColor: Boolean = false,
    content: @Composable () -> Unit
) {
    BudgetAppTheme(darkTheme, dynamicColor, content)
}